//
//  BGSellerGodBidController.m
//  bigMall
//
//  Created by LTOVE on 16/9/24.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGSellerGodBidController.h"

#import "BGSellerGoBidParam.h"  //请求参数模型

#import "BGSellerGoBidCell.h"

#import "BGSellerGoBidCellModel.h"

#import "LTOVEDynamicCreakButton.h"

#import "MJExtension.h"
#import "BGDateResult.h"
#import "BGCalculateMarginModel.h"
#import "BGLoginViewController.h"

#import "BGSellerGoBidModel.h"

@interface BGSellerGodBidController ()<dynamicButtonDelegate,BGSellerGoBidCellDelegate>{
    
    NSMutableArray *dataArray;
    LTOVEDynamicCreakButton *btnView;
    CGFloat num;
    CGFloat price;
}

@end

@implementation BGSellerGodBidController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self initWithData];
//    [self.tableView reloadData];
    self.tableView.height = self.tableView.height - 70;
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero]; 
//    [self addDynacBtn];
}

- (void)setModel:(BGSellerGoBidParam *)model
{
    _model = model;
    [self initWithDataWithModel:model];
    [self.tableView reloadData];
}

- (void)addDynacBtn
{
    NSDictionary *dic = @{
                          NSFontAttributeName:Font34,
                          NSForegroundColorAttributeName:lightwhite_
                          };
    LTOVEDynamicCreakButton *dynacBtn = [LTOVEDynamicCreakButton buttonViewWithFrame:CGRectMake(0, 100, screenW, 70) titleDataArray:@[@"报价"] andEdgInset:UIEdgeInsetsMake(10, BGMaginLeft, 10, BGMaginLeft) andMaginX:0 andMaginY:0 andBtnHeigh:50 andBtnNorColor:read_ andBtnHeighColor:read_ andRadius:5 andFont:dic];
    dynacBtn.y = screenH - 70;
    btnView = dynacBtn;
    dynacBtn.delegate = self;
    [BGKeyWindow addSubview:dynacBtn];
//    [self.view addSubview:dynacBtn];
    
}
/**
 *  点击了拍卖按钮
 *
 *  @param btn btn description
 */
- (void)dynamicButtonClick:(UIButton *)btn {
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入支付密码" preferredStyle:UIAlertControllerStyleAlert];
    [alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
       textField.placeholder = @"请输入支付密码";
        textField.secureTextEntry = YES;
    }];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        UITextField *textFild = [alertVc.textFields firstObject];
        self.model.payPassword = textFild.text;
#pragma mark 提交下单
        [self addList];
    }];
    [alertVc addAction:sure];
    
    [self presentViewController:alertVc animated:YES completion:^{
        
        
    }];
    
}

- (void)addList{
    BGSellerGoBidParam *param = self.model;
    param.MaginScal = nil;
    [BGDateResult sellerGoBidWithParamDic:param.keyValues success:^(NSObject *result) {
        BGSellerGoBidModel *resultModel = (BGSellerGoBidModel *)result;
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:resultModel.msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if ([resultModel.status isEqualToString:@"1"]) {
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }];
        [alertVc addAction:sure];
        [self presentViewController:alertVc animated:YES completion:^{
            
            
        }];
    } failure:^(NSError *error) {
        
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.tableView endEditing:YES];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
}




- (void)viewWillAppear:(BOOL)animated
{
    [self addDynacBtn];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [btnView removeFromSuperview];
}

/**
 *  初始化数据
 */
- (void)initWithDataWithModel:(BGSellerGoBidParam *)model

{
    //竞标数量；
    BGSellerGoBidCellModelTextFild *bidNumModel = [BGSellerGoBidCellModelTextFild new];
    bidNumModel.title = @"竞标数量";
    bidNumModel.placehoder = [NSString stringWithFormat:@"至少供应量%@",model.number];
    dataArray = [NSMutableArray arrayWithObject:bidNumModel];
    //竞标单价
    BGSellerGoBidCellModelTextFild *bidPriceModel = [BGSellerGoBidCellModelTextFild new];
    bidPriceModel.title = @"竞标单价";
    bidPriceModel.placehoder = @"请输入竞标单价";
    [dataArray addObject:bidPriceModel];
    //支付密码
    BGSellerGoBidCellModelTextFild *bidPriceModels = [BGSellerGoBidCellModelTextFild new];
    bidPriceModels.title = @"备注";
    bidPriceModels.placehoder = @"备注内容";
    [dataArray addObject:bidPriceModels];
    //总价
    BGSellerGoBidCellModelTextCell *totPriceModel = [BGSellerGoBidCellModelTextCell new];
    totPriceModel.totalPrice = @"";
    totPriceModel.magin = @"";
    [dataArray addObject:totPriceModel];
//    [dataArray addObjectsFromArray:dataArray];
//    [dataArray addObjectsFromArray:dataArray];
//    [dataArray addObjectsFromArray:dataArray];
//    [dataArray addObjectsFromArray:dataArray];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BGSellerGoBidCell *cell = [BGSellerGoBidCell cellWithTableView:tableView];
    // Configure the cell...
    BGSellerGoBidCellModel *model = dataArray[indexPath.row];
    model.indexPath = indexPath;
    cell.delegate = self;
    cell.model = model;
    
    return cell;
}


- (void)cellValueDidChangedWithModel:(BGSellerGoBidCellModel *)model
{
//    GWLog(@"%@",model.indexPath.row);
    BGSellerGoBidCellModelTextFild *itemModel = (BGSellerGoBidCellModelTextFild *)model;
    if ([itemModel.title isEqualToString:@"竞标数量"]) {
        CGFloat nums = [itemModel.textFildStr floatValue];
        num = nums;
        self.model.number = [NSString stringWithFormat:@"%.3f",nums];
        [self loadTotalPrice];
    }else if ([itemModel.title isEqualToString:@"竞标单价"]){
        CGFloat prices = [itemModel.textFildStr floatValue];
        price = prices;
        self.model.price = [NSString stringWithFormat:@"%.2f",prices];
        [self loadTotalPrice];
    }else if ([itemModel.title isEqualToString:@"备注"]){
        self.model.remark = itemModel.textFildStr;
    }
    
}

- (void)loadTotalPrice
{
    if (!num || !price) {
        return;
    }
    CGFloat totalPrice = num * price;
    CGFloat maginScal = [self.model.MaginScal floatValue];
    NSString *maginValue = [NSString stringWithFormat:@"%.2f",totalPrice * maginScal / 100];
    
//    self.model.amount = [NSString stringWithFormat:@"%.2f",totalPrice];
//    self.model.margin = maginValue;
//    [self.tableView reloadData];

    BGSellerGoBidCellModelTextCell *model = [BGSellerGoBidCellModelTextCell new];
    model.totalPrice = [NSString stringWithFormat:@"%.2f",totalPrice];
    model.magin = maginValue;
    self.model.amount = model.totalPrice;
    self.model.margin = model.magin;
    [dataArray replaceObjectAtIndex:dataArray.count - 1 withObject:model];
    [self.tableView reloadData];
}


@end
